package com.weactivity.controller;

import com.weactivity.dto.ContactDTO;
import com.weactivity.dto.Result;
import com.weactivity.service.ContactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 通讯录controller
 */

@RestController
@RequestMapping("/api/contact")
public class ContactController {

    @Autowired
    private ContactService contactService;

    @GetMapping("/showContact")
    public Result showContact(){

        //根据用户id查找用户名下的联系人
      List<ContactDTO> contactDTOList =  contactService.showContact();
      if (null == contactDTOList){
          return Result.ok();
      }


      return Result.ok(contactDTOList, Long.valueOf(contactDTOList.size()));
    }

    /**
     * 重命名
     * @param contactDTO
     * @return
     */
    @PostMapping("/remark")
    public Result remark(@RequestBody ContactDTO contactDTO){

      int count =   contactService.remark(contactDTO);
      if (0 == count){
          return Result.fail("重命名失败");
      }
      return Result.ok();
    }

    @PostMapping("/delete")
    public Result deleteMate(@RequestBody Map<String,Object> payload){

        int count = contactService.deleteById((Integer) payload.get("mateId"));
        if (0 == count){
            return Result.fail("删除失败");
        }

        return Result.ok();
    }





}
